//二分查找
function search(target, array) {
    let left = 0
    let right = array.length - 1
    let mid = (left + right) / 2

    while (left <= right) {
        mid = Math.floor((left + right) / 2)

        if (target < array[mid]) {
            right = mid - 1
        }
        else if (target > array[mid]) {
            left = mid + 1
        }
        else if (target == array[mid]) {
            return mid
        }
    }
    return '没有元素'
}
let array = [0, 1, 2, 3, 4, 5, 6, 7, 8]

console.log('1.二分查找------------------------------');
console.log(search(5, array));
console.log('--------------------------------------');